﻿/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Text;
using ASN1Decoder.Builders.Nodes;
using Core.Model;
using Exceptions;
using Core.Model.Groups;
using Core.Model.Ingredients.Presentable.TokenManagers;

namespace ASN1Decoder.Builders
{
    internal class TokenGroupBuilder : IngredientBuilder
    {
        internal static TokenGroup Build(TaggedNode node, Group group)
        {
            var tokenGroup = new TokenGroup();
            IngredientBuilder.Build(node, tokenGroup, group);

            TaggedNode e;

            if ((e = node[ASN1ContextCodes.C_MOVEMENT_TABLE]) != null)
            {
                var movementTable = new List<List<int>>();

                List<int> movementTableRow;

                foreach (SequenceNode row in e)
                {
                    movementTableRow = new List<int>();

                    foreach (IntNode col in row)
                    {
                        movementTableRow.Add(col.IntValue);
                    }
                    
                    movementTable.Add(movementTableRow);
                }

                tokenGroup.MovementTable = movementTable;
            }

            if ((e = node[ASN1ContextCodes.C_TOKEN_GROUP_ITEMS]) != null)
            {
                foreach (SequenceNode tgi in e)
                {
                    var tokenGroupItem = new TokenGroupItem();
                    tokenGroupItem.ObjectRef = ObjectRefBuilder.Build(tgi.First, group);

                    if (tgi.Size > 1)
                    {
                        foreach (ASN1Node slot in (SequenceNode)tgi.Second)
                        {
                            if (slot is SequenceNode)
                                tokenGroupItem.ActionSlots.Add(ActionBuilder.Build(slot, group));
                            else if (slot is NullNode)
                                tokenGroupItem.ActionSlots.Add(null);
                            else 
                                throw new OpenMhegException("Unkown Node");
                        }
                    }

                    tokenGroup.TokenGroupItems.Add(tokenGroupItem);
                }
            }
            else
                throw new OpenMhegException("No token group items");

            if ((e = node[ASN1ContextCodes.C_NO_TOKEN_ACTION_SLOTS]) != null)
            {
                foreach (ASN1Node slot in e)
                {
                    if (slot is SequenceNode)
                        tokenGroup.NoTokenActionSlots.Add(ActionBuilder.Build(slot, group));
                    else if (slot is NullNode)
                        tokenGroup.NoTokenActionSlots.Add(null);
                    else
                        throw new OpenMhegException("Unkown Node");
                }

            }

            return tokenGroup;
        }
    }
}
